package com.tool.cn.excels;

import com.alibaba.excel.EasyExcel;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class JsonToExcel {
    public static void main(String[] args) {
        String inputDir = "C:\\Users\\28020\\Desktop\\data\\medJson"; // 替换为你的 JSON 文件夹路径
        String outputExcelPath = "output.xlsx";

        List<ResultDataEntity> dataList = new ArrayList<>();

        ObjectMapper objectMapper = new ObjectMapper();

        try {
            // 遍历文件夹中的所有 .json 文件
            Files.list(Paths.get(inputDir))
                    .filter(path -> path.toString().endsWith(".json"))
                    .forEach(jsonFilePath -> {
                        try {
                            // 读取 JSON 文件内容
                            String content = new String(Files.readAllBytes(jsonFilePath));
                            JsonNode jsonNode = objectMapper.readTree(content);

                            // 提取文件名作为 name
                            String name = jsonFilePath.getFileName().toString().replace(".json", "");

                            // 判断文件是否有数据
                            int code = jsonNode.get("code").asInt();
                            if (code == 0) {
                                ResultDataEntity entity = new ResultDataEntity();
                                entity.setName(name);
                                entity.setDiseaseName("");
                                entity.setMedicalType("");
                                entity.setMedicalAmount("");
                                entity.setHospitalName("");
                                entity.setMedicalDept("");
                                entity.setMedicalDate("");
                                dataList.add(entity);
                            } else {
                                // 提取 resultData 数组
                                JsonNode resultDataNode = jsonNode.at("$.data.payload.resultData");
                                if (resultDataNode.isArray()) {
                                    for (JsonNode item : resultDataNode) {
                                        ResultDataEntity entity = new ResultDataEntity();
                                        entity.setName(name);
                                        entity.setDiseaseName(item.path("diseaseName").asText());
                                        entity.setMedicalType(item.path("medicalType").asText());
                                        entity.setMedicalAmount(item.path("medicalAmount").asText());
                                        entity.setHospitalName(item.path("hospitalName").asText());
                                        entity.setMedicalDept(item.path("medicalDept").asText());
                                        entity.setMedicalDate(item.path("medicalDate").asText());
                                        dataList.add(entity);
                                    }
                                }
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    });

            // 写入 Excel
            EasyExcel.write(outputExcelPath, ResultDataEntity.class)
                    .sheet("Sheet1")
                    .doWrite(dataList);

            System.out.println("Excel 文件已生成: " + outputExcelPath);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}